package cn.wolfcode.trip.app.web.controller;


import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.OfComment;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.OfCommentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService catalogService;
    @Autowired
    private OfCommentMapper ofCommentMapper;
    @Autowired
    private ServletContext ctx;

    //查询某大攻略下的所有目录
    @GetMapping("/{strategyId}/catalog")
    @ResponseBody
    public Object getCatalongByStrategyId(@PathVariable Long strategyId){
        List<StrategyCatalog> list = catalogService.getStrategyCatalogByStrategyId(strategyId);
        return list;
    }
    //用于查询某大攻略的信息
    @GetMapping("/{strategyId}")
    @ResponseBody
    public Object get(@PathVariable Long strategyId){
        return strategyService.get(strategyId);
    }

    //找攻略-推荐-当季推荐-攻略显示
    @GetMapping
    @ResponseBody
    public Object list(StrategyQueryObject qo){
        qo.setPageSize(0);
        PageInfo pageInfo = strategyService.listByState(qo);
        return pageInfo;
    }

    @GetMapping("/{strategyId}/tags")
    @ResponseBody
    public Object listTagsByStrategyId(@PathVariable Long strategyId, QueryObject qo){
        return strategyService.getTagsByStrategyId(strategyId,qo);
    }

    @PostMapping("/strategyComments")
    @ResponseBody
    public Object saveStrategyComments(Comment comment,String myTags,Long[] ids){
        strategyService.saveStrategyComments(comment,myTags,ids);
        return new JSONResult();
    }

    @PostMapping("/strategyOfComments")
    @ResponseBody
    public Object saveStrategyOfComments(OfComment ofComment){
        strategyService.saveStrategyOfComments(ofComment);
        return new JSONResult();
    }

    OfComment temp;

    @PostMapping("/strategyOfOfComments")
    @ResponseBody
    public Object saveStrategyOfOfComments(OfComment ofComment) {

        strategyService.saveStrategyOfOfComments(ofComment);
        String tempContent = ofCommentMapper.selectTempContentByCommentId(ofComment.getParentId());
        Date tempDate =  ofCommentMapper.selectTempDateByCommentId(ofComment.getParentId());
        temp = ofCommentMapper.selectByPrimaryKey(ofComment.getId());
        temp.setTempContent(tempContent);
        temp.setTempDate(tempDate);
        return new JSONResult();
    }

    @GetMapping("/temps")
    @ResponseBody
    public void getTemp(HttpServletResponse resp) throws Exception {

        PageInfo pageInfo = new PageInfo(Arrays.asList(temp));

        //通过Freemarker配置对象进行模板数据的渲染
        Configuration cf = new Configuration(Configuration.VERSION_2_3_28);
        cf.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/templates")));
        //获取模板文件
        Template template = cf.getTemplate("ofofcomment.ftl","UTF-8");

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("pageInfo",pageInfo);
        template.process(map, resp.getWriter());
    }

    @GetMapping("/strategyComments/{strategyCommentId}")
    @ResponseBody
    public Object getStrategyCommentByStrategyCommentId(@PathVariable Long strategyCommentId,QueryObject qo){

        return strategyService.getStrategyCommentByStrategyCommentId(strategyCommentId,qo);
    }

    //评论的点赞
    @GetMapping("/strategyCommentLikers/{strategyCommentId}/{likerId}")
    @ResponseBody
    public Object getStrategyCommentLikersByStrategyCommentId(@PathVariable Long strategyCommentId,@PathVariable Long likerId){

        return strategyService.showStrategyCommentLikersByStrategyCommentId(strategyCommentId,likerId);
    }

    //子评论的点赞
    @GetMapping("/strategyOfCommentLikers/{strategyOfCommentId}/{likerId}")
    @ResponseBody
    public Object getStrategyOfCommentLikersByStrategyCommentId(@PathVariable Long strategyOfCommentId,@PathVariable Long likerId){

        return strategyService.showStrategyOfCommentLikersByStrategyCommentId(strategyOfCommentId,likerId);
    }


    @GetMapping("/strategyOfComments/{strategyCommentId}")
    @ResponseBody
    public void getStrategyOfCommentByStrategyCommentId(@PathVariable Long strategyCommentId,QueryObject qo,
                                                        HttpServletResponse resp) throws Exception {

        //通过Freemarker配置对象进行模板数据的渲染
        Configuration cf = new Configuration(Configuration.VERSION_2_3_28);
        cf.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/templates")));
        //获取模板文件
        Template template = cf.getTemplate("ofcomment.ftl","UTF-8");

        PageInfo pageInfo = strategyService.getStrategyOfCommentByStrategyCommentId(strategyCommentId, qo);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("pageInfo",pageInfo);
        template.process(map, resp.getWriter());

    }

    @GetMapping("/{strategyId}/strategyComments")
    @ResponseBody
    public Object listStrategyCommentsByStrategyId(@PathVariable Long strategyId,QueryObject qo){
        return strategyService.getStrategyCommentsByStrategyId(strategyId,qo);
    }

}
